package com.tadpole.music.presenter.question;

import com.tadpole.music.bean.question.TrainingQuestionBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.question.TrainingQuestionIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 体验/模拟训练
 * Created By 孙岩
 * on 2020/9/15
 */
public class TrainingQuestionPresenter extends BasePresenter<TrainingQuestionIView> {

    /**
     * @param page              页码
     * @param college_id        学院
     * @param question_rank_id  等级
     * @param question_class_id 分类
     * @param isTry             是否是体验 1是 0 否
     */
    @SuppressWarnings("unchecked")
    public void getTrainingQuestion(int page, String college_id, String question_rank_id, String question_class_id, int isTry) {
        if (isViewAttached()) {
            String url = UrlConfig.trainingQuestion + "?page=" + page + "&college_id=" + college_id + "&question_rank_id=" + question_rank_id + "&question_class_id=" + question_class_id + "&try=" + isTry;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject object = new JSONObject(data);
                                if (object.getInt("code") == 200) {
                                    JSONObject jsonObject = new JSONObject(object.getString("data"));
                                    List<TrainingQuestionBean.DataBeanX.DataBean> list = new ArrayList<>();
                                    JSONArray array = new JSONArray(jsonObject.getString("data"));
                                    for (int i = 0; i < array.length(); i++) {
                                        JSONObject json = new JSONObject(array.get(i).toString());
                                        TrainingQuestionBean.DataBeanX.DataBean bean = new TrainingQuestionBean.DataBeanX.DataBean(
                                                JsonParseUtil.getInt(json, "id"),
                                                JsonParseUtil.getStr(json, "created_at"),
                                                JsonParseUtil.getStr(json, "updated_at"),
                                                JsonParseUtil.getStr(json, "title"),
                                                JsonParseUtil.getInt(json, "college_id"),
                                                JsonParseUtil.getInt(json, "question_rank_id"),
                                                JsonParseUtil.getInt(json, "question_class_id"),
                                                JsonParseUtil.getInt(json, "question_type_id"),
                                                JsonParseUtil.getInt(json, "status"),
                                                JsonParseUtil.getStr(json, "title_img"),
                                                JsonParseUtil.getStr(json, "title_sound"),
                                                JsonParseUtil.getStr(json, "bg_sound"),
                                                JsonParseUtil.getInt(json, "play_times"),
                                                JsonParseUtil.getInt(json, "interval"),
                                                JsonParseUtil.getInt(json, "answer_type"),
                                                JsonParseUtil.getStr(json, "answer_a"),
                                                JsonParseUtil.getStr(json, "answer_b"),
                                                JsonParseUtil.getStr(json, "answer_c"),
                                                JsonParseUtil.getStr(json, "answer_d"),
                                                JsonParseUtil.getStr(json, "answer_e"),
                                                JsonParseUtil.getStr(json, "answer_f"),
                                                JsonParseUtil.getStr(json, "answer_g"),
                                                JsonParseUtil.getStr(json, "answer_h"),
                                                JsonParseUtil.getInt(json, "right_answer"),
                                                JsonParseUtil.getStr(json, "extra"),
                                                JsonParseUtil.getBoolean(json, "is_multy_answer"),
                                                JsonParseUtil.getStr(json, "multy_right_answer"),
                                                JsonParseUtil.getStr(json, "left_drum"),
                                                JsonParseUtil.getStr(json, "right_drum")
                                        );
                                        list.add(bean);
                                    }
                                    TrainingQuestionBean.DataBeanX bean = new TrainingQuestionBean.DataBeanX(
                                            JsonParseUtil.getInt(jsonObject, "current_page"),
                                            JsonParseUtil.getInt(jsonObject, "total"),
                                            JsonParseUtil.getInt(jsonObject, "page_size"),
                                            JsonParseUtil.getInt(jsonObject, "last_page"),
                                            list
                                    );
                                    getView().showQuestion(bean);

                                } else if (object.getInt("code") == 403){
                                    getView().goToPay();
                                } else if(object.getInt("code")==401){
                                    getView().go401();
                                } else {
                                    ToastUtils.show(object.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {
                            getView().hideLoading();
                        }

                        @Override
                        public void onBefore() {
                            getView().showLoading();
                        }
                    });
        }
    }
}
